package com.apanal.qlife.advice.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apanal.qlife.advice.model.Advice;
import com.apanal.qlife.advice.service.SysAnnounceService;
import com.apanal.qlife.advice.service.SysInformService;
import com.apanal.qlife.common.annotation.CurrentUser;
import com.apanal.qlife.common.annotation.LogType;
import com.apanal.qlife.common.annotation.SystemLog;
import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.common.util.IdWorkerFactory;
import com.apanal.qlife.org.model.Organization;
import com.apanal.qlife.org.service.OrganizationService;
import com.apanal.qlife.sys.model.Records;
import com.apanal.qlife.sys.model.User;

@Controller
@RequestMapping("/inform")
public class SysInformController {

	@Autowired
	private SysInformService sysInformService;

	@Autowired
	private SysAnnounceService sysAnnounceservice;

	@Autowired
	private OrganizationService orgService;

	/**
	 * 查询所有已发布系统公告
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "系统通知管理", operation = "分页查询系统已发布通知", type = LogType.SELECT)
	@RequiresPermissions("advice:inform:view")
	@RequestMapping(value = "list")
	public String list(Model model, Advice advice, Page page,
			HttpServletRequest request, @CurrentUser User user) {

		PageReturn<Advice> pr = sysInformService.findInformBycond(advice, page);
		model.addAttribute("result", pr);
		model.addAttribute("advice", advice);
		return "advice/sysInfromList";
	}

	/**
	 * 查询所有未发布系统公告
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "系统通知管理", operation = "分页查询系统未发布通知", type = LogType.SELECT)
	@RequiresPermissions("advice:inform:view")
	@RequestMapping(value = "listNo")
	public String listNo(Model model, Advice advice, Page page) {
		PageReturn<Advice> pr = sysInformService.findInformBycondNo(advice, page);
		model.addAttribute("result", pr);
		model.addAttribute("advice", advice);
		System.out.println("=============");
		return "advice/sysInfromListNo";
	}

	/**
	 * 新增系统公告
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("advice:inform:create")
	@RequestMapping(value = "informAdd", method = RequestMethod.GET)
	public String add(Model model) {

		return "advice/informAdd";
	}

	@RequestMapping(value = "/{selectType}/chooseOrg", method = RequestMethod.GET)
	public String chooseOrg(Model model,
			@PathVariable("selectType") Integer selectType) {
		// System.out.println("++++++"+selectType);
		model.addAttribute("selectType", selectType);// 所选择的投放方式
		// 查找所有平台
		List<Organization> platList = orgService.findAllPlat();
		// 查找渠道
		List<Organization> qdList = orgService.findAllQd();
		model.addAttribute("platList", platList);
		model.addAttribute("qdList", qdList);
		String sueccess = "";
		if (selectType == 12) {
			sueccess = "advice/chooseOrg"; // 选择平台
		} else if (selectType == 13) {
			sueccess = "advice/chooseQd"; // 选择渠道
		} else if (selectType == 14) {
			sueccess = "advice/chooseWy"; // 选择物业
		} else if (selectType == 15) {
			sueccess = "app/chooseCommity"; // 选择社区
		} else if (selectType == 22) {
			sueccess = "app/chooseProvince"; // 选择省
		} else if (selectType == 23) {
			sueccess = "app/chooseCity"; // 选择市
		} else if (selectType == 24) {
			sueccess = "app/chooseAera"; // 选择县，区
		}
		return sueccess;
	}

	/**
	 * 查看通知详情查看
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("advice:inform:view")
	@RequestMapping(value = "/{advId}/listDetails")
	public String listDetails(@CurrentUser User user, Model model, Advice advice) {
		// 查看当前登录人是否首次访问
		int a = sysInformService
				.findReadLog(user.getUseId(), advice.getAdvId());
		Advice enty = sysInformService.findAnnounceById(advice.getAdvId());
		if (a == 0) {
			// records标志为已读，数据库加1
			Records rc = new Records();
			rc.setForeignId(advice.getAdvId());
			//rc.setInsertDate(new Date());
			//rc.setRecFlag(1);
			rc.setRecFlag(Constants.REC_FLAG_READ);
			//rc.setRecType(3);
			rc.setRecType(Constants.REC_TYPE_SYS_INFORM);
			rc.setUseId(user.getUseId());
			//rc.setLastUpdateDate(new Date());
			rc.setRecId(IdWorkerFactory.nextId());
			sysInformService.insertRecords(rc);
			enty.setTotalCount(enty.getTotalCount() + 1);
			sysInformService.updateTotalCount(enty);
		}
		Advice ac = sysAnnounceservice.findAppAnnouceDtail(advice);
		model.addAttribute("advice", ac);
		return "advice/listDetails";
	}

	/**
	 * 新增系统通知保存
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "系统通知管理", operation = "新增系统通知", type = LogType.CREATE)
	@RequiresPermissions("advice:inform:create")
	@ResponseBody
	@RequestMapping(value = "saveInform", method = RequestMethod.POST)
	public String saveInform(@CurrentUser User user, Model model, Advice advice)
			throws ParseException {

		sysInformService.insertAdvice(advice,user);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * 编辑系统通知
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("advice:inform:update")
	@RequestMapping(value = "/{advId}/informEdit")
	public String informEdit(@CurrentUser User user, Model model, Advice advice) {
		Advice ac = sysAnnounceservice.findAppAnnouceDtail(advice);
		model.addAttribute("advice", ac);
		return "advice/informEdit";

	}

	/**
	 * 编辑系统通知保存
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "系统通知管理", operation = "修改系统通知", type = LogType.UPDATE)
	@RequiresPermissions("advice:inform:update")
	@ResponseBody
	@RequestMapping(value = "SaveEditInform", method = RequestMethod.POST)
	public String SaveEditInform(@CurrentUser User user, Model model,
			Advice advice) throws ParseException {

//		// 更新公告表
		Advice ac = sysInformService.findInformDetailById(advice.getAdvId());
		sysInformService.updateInform(ac,user,advice);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * 系统通知发送
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "系统通知管理", operation = "发送系统通知", type = LogType.UPDATE)
	@RequiresPermissions("advice:inform:send")
	@RequestMapping(value = "/{advId}/SendInform")
	public String SendInform(@CurrentUser User user, Model model, Advice advice)
			throws ParseException {

		// 更新公告表
		Advice ac = sysInformService.findInformDetailById(advice.getAdvId());
		ac.setStartDate(new Date());
		ac.setLastUpdateBy(user.getUseId());
		sysInformService.modifyInform(ac);
		return "redirect:/inform/list";
	}

	/**
	 * 系统通知关闭
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "系统通知管理", operation = "关闭系统通知", type = LogType.DELETE)
	@RequiresPermissions("advice:inform:delete")
	@ResponseBody
	@RequestMapping(value = "/{advId}/CloseInform")
	public String CloseInform(@CurrentUser User user, Model model, Advice advice)
			throws ParseException {

		// 更新公告表
		//Advice ac = sysInformService.findInformDetailById(advice.getAdvId());
		//ac.setStatus(Constants.IS_VALID_FALSE);
		//ac.setLastUpdateBy(user.getUseId());
		sysInformService.deleteInform(advice);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

}
